/*
	Copyright (C) 2011 Salil Bhagurkar

	This file is part of illusion

	illusion is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation, either version 3 of
	the License, or (at your option) any later version.

	illusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with illusion. 
	If not, see <http://www.gnu.org/licenses/>.
*/

#include <klib/lib.h>
#include <kernel/console.h>
#include <kernel/irq.h>
#include <kernel/errors.h>

/*
 * Kernel IRQ management. IRQs come here from the arch code and roam about.
 */

#define NR_IRQ 16

static int (*irq_handlers[NR_IRQ])() = {0};

int irq_set_handler(uint_t irq, int (*irq_handler)())
{
	initmsg("IRQ %u: 0x%x\n", irq, irq_handler);
	if(irq_handlers[irq] != null)
		return ESTINV;
	irq_handlers[irq] = irq_handler;
	return 0;
}

int irq_remove_handler(uint_t irq)
{
	if(irq_handlers[irq] == null)
		return ESTINV;
	irq_handlers[irq] = null;
	return 0;
}

int handle_irq(uint_t irq_no)
{
	if(irq_handlers[irq_no] != null) {
		return irq_handlers[irq_no]();
	}
	return 0;
}
